#include "netdisk3.h"

int isInThisList(char *arg){
    DIR* drip=opendir(arg);
    struct dirent* dire;
    dire=readdir(drip);
    while(dire!=NULL){
        if(strcmp(dire->d_name,arg)==0){
            return 1;
        }
        dire=readdir(drip);
    }
    closedir(drip);
    return 0;
}

void cutstr(char * input,char *filename){
    //char filename[256]; // 假定文件名不超过256个字符

    char *filename_start = strrchr(input, '/');
    if (filename_start != NULL) { // 如果发现'/'，则文件名为'/'之后的内容
        filename_start++; // 跳过'/'，指向文件名的起始位置
    } else {
        filename_start = input; // 否则整个字符串都是文件名
    }

    strcpy(filename, filename_start); // 复制文件名到新的字符数组中
   // printf("%s\n",filename);
    //return filename;
}
int getsTextFunc(const char* arg, int retM,int netfd ){//输入差一个栈参数，后面可以补上
    //char* ptext = cutstr(arg);
    //char ptext[256];
    //cutstr(arg,ptext);//ptext为要传输的文件的文件名
    //int num = strlen(ptext);
    
    // train_name_t train;
    
    //train.length=num;
    //strcpy(train.data,ptext);
    //send(netfd,&train,sizeof(train.length),MSG_NOSIGNAL);
    //send(netfd,&train.data,train.length,MSG_NOSIGNAL);
    char real_path[4096] = {0};
    // onePath(arg, real_path, &dir);
    
    // recvn(netfd,&train.length,sizeof(train.length));//接收文件名火车头
    // recvn(netfd,&train.offset,sizeof(train.offset));
    // recvn(netfd,train.data,train.length);//接收文件火车箱

    off_t offset = 0;
    recvn(netfd, &offset, sizeof(off_t));
    int fd = open(arg, O_RDWR);//open第一个参数为绝对地址
    if(retM==-1){
        tran_t errorMsg;
        bzero(&errorMsg, sizeof(tran_t));
        strcpy(errorMsg.data, "file not exist\n");
        errorMsg.length = strlen(errorMsg.data);
        
        send(netfd, &fd, sizeof(int),MSG_NOSIGNAL);
        send(netfd, &errorMsg, sizeof(int) + errorMsg.length, MSG_NOSIGNAL);
        close(fd);
        return -1;//返回错误类型为未打开文件
    }else{
        int ret = 0;
        send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
    }
    struct stat statbuf;
    fstat(fd,&statbuf);
    //train.length=sizeof(statbuf.st_size);
    //memcpy(train.data,&statbuf.st_size,train.length);
    //send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    
    tran_t train;
    bzero(&train, sizeof(train));

    if(statbuf.st_size > 1000000)
	{
        //int offset = train.offset;
		char *p = (char *)mmap(NULL,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
		ERROR_CHECK(p,MAP_FAILED,"mmap");
		off_t totalsize = offset;
		while(totalsize < statbuf.st_size)
		{
			if(statbuf.st_size - totalsize > (off_t)sizeof(train.data))
				train.length = 4096;
			else
				train.length = statbuf.st_size - totalsize;
			send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
            // send(netfd,&train.offset,sizeof(train.offset),MSG_NOSIGNAL);
			send(netfd,p+totalsize,train.length,MSG_NOSIGNAL);
			totalsize += train.length;
		}
	}
	else
    {
    lseek(fd, offset,SEEK_SET);
        while(1){
            ssize_t sret=read(fd,train.data,sizeof(train.data));
            if(sret==0){
                break;
            }
            train.length = sret;
            // send(netfd, &train.length, sizeof(train.length), MSG_NOSIGNAL);
            sret = send(netfd, &train, sizeof(int)+train.length,MSG_NOSIGNAL);
        }
    }
    printf("all send over\n");
    train.length=0;
    send(netfd,&train,sizeof(train.length),MSG_NOSIGNAL);
    close(fd);
    return 0;
}


